package com.zhuzi.webservice;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.thread.ThreadUtil;
import com.alibaba.fastjson.JSONObject;
import com.zhuzi.dto.GatherInfo;
import com.zhuzi.utils.ClientUtil;
import com.zhuzi.utils.XmlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.jws.WebService;
import java.time.LocalDateTime;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author zhuzi
 * 2022/9/19 01:08
 */
@Slf4j
@Component
@WebService(serviceName = "parseXml", // 与接口中指定的name一致
        targetNamespace = "http://webservice.zhuzi.com/", // 与接口中的命名空间一致,一般是接口的包名倒
        endpointInterface = "com.zhuzi.webservice.ParseXml"// 接口地址
)
public class ParseXmlRelaImpl implements ParseXml {
    private final static String RESP_SUCCESS = new JSONObject().fluentPut("success", "0").fluentPut("msg", "报文解析成功").toJSONString();
    @Value("${webservice.url}")
    private String url;
    @Value("${result}")
    private String isPass;
    private String str82 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
            "<GATHER_FEEDBACK\n" +
            "    xmlns=\"http://rpb.customs.gov.cn/common/FrontEndSchemas/\"\n" +
            "    xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" +
            "    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
            "    <AREA_ID>%s</AREA_ID>\n" +
            "    <CHNL_NO>%s</CHNL_NO>\n" +
            "    <SESSION_ID>%s</SESSION_ID>\n" +
            "    <RELLIST_ID_TYPE>运输批次号</RELLIST_ID_TYPE>\n" +
            "    <FEEDBACK_TIME>2022-09-15T11:55:41.1410726+08:00</FEEDBACK_TIME>\n" +
            "    <CHECK_RESULT>Y</CHECK_RESULT>\n" +
            "    <INSTRUCTION/>\n" +
            "    <PROC_ERROR_CODE>KKPE001</PROC_ERROR_CODE>\n" +
            "    <PROC_ERROR_DESCRIPTION>蒙AF820R, 未找到电子车牌E2801105200078D58FAF0AC0的备案信息！</PROC_ERROR_DESCRIPTION>\n" +
            "    <TECH_ERROR_CODE/>\n" +
            "    <TECH_ERROR_DESCRIPTION/>\n" +
            "    <VE_INFO>\n" +
            "        <VE_LICENSE_NO>蒙AF820R</VE_LICENSE_NO>\n" +
            "    </VE_INFO>\n" +
            "    <CONTA_ID/>\n" +
            "    <ESEAL_ID/>\n" +
            "    <OP_HINT>有电子车牌的备案信息</OP_HINT>\n" +
            "    <LED_HINT>海关不放行</LED_HINT>\n" +
            "    <EXTENDED_CONTENT/>\n" +
            "</GATHER_FEEDBACK>";


    @Override
    public String parseSecondTierXml(String context, String type) {
        log.info("接收到信息[{}][{}]", context, type);

        CompletableFuture.runAsync(() -> {

            ThreadUtil.sleep(6, TimeUnit.SECONDS);
            GatherInfo gatherInfo = XmlUtils.jacksonXML2Bean(context, GatherInfo.class);

            String chnlNo = gatherInfo.getChnlNo();
            String areaId = gatherInfo.getAreaId();
            String sessionId = gatherInfo.getSessionId();
            System.out.println("Calling" + url);
            String result = "";
            try {
                String led = "海关放行";
                String ledHint = "放行";
                if (!StringUtils.equalsIgnoreCase("y", isPass)) {
                    ledHint = "没有电子车牌的备案信息";
                    led = "不放行";
                }
                //result = ClientUtil.callWebSV(url, "Receive82Xml", String.format(str82, areaId, chnlNo, sessionId, isPass, ledHint, led), "0x82");
                result = ClientUtil.callWebSV(url, "Receive82Xml", String.format(str82, areaId, chnlNo, sessionId, isPass), "0x82");
            } catch (Exception e) {
                System.err.println("接口调用失败！！！！");
                e.printStackTrace();

            }
            System.out.println("===Finished!===恭喜你啊！！！CatService接口调用成功！！！===获得的数据是：" + result);

        });
        //{"messageTime":"2022-09-15 12:01:35","":"true","recMessage":"上传成功"}
        return new JSONObject().fluentPut("recStatus", "true").fluentPut("messageTime", LocalDateTimeUtil.formatNormal(LocalDateTime.now())).fluentPut("recMessage", "上传成功").toJSONString();
    }

    public static void main(String[] args) throws Exception {
        String url = "http://127.0.0.1:8182/Service/parseXml?wsdl";
        String cont = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<GATHER_FEEDBACK xmlns=\"http://rpb.customs.gov.cn/common/FrontEndSchemas/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
                "  <AREA_ID>CNCEK070127</AREA_ID>\n" +
                "  <CHNL_NO>CNCEK070127WI06</CHNL_NO>\n" +
                "  <SESSION_ID>CNCEK070127WI0620230107223550</SESSION_ID>\n" +
                "  <RELLIST_ID_TYPE>运输批次号</RELLIST_ID_TYPE>\n" +
                "  <FEEDBACK_TIME>2023-01-07T22:35:57.0937124+08:00</FEEDBACK_TIME>\n" +
                "  <CHECK_RESULT>N</CHECK_RESULT>\n" +
                "  <INSTRUCTION/>\n" +
                "  <PROC_ERROR_CODE>KKPE001</PROC_ERROR_CODE>\n" +
                "  <PROC_ERROR_DESCRIPTION>Test001, 未找到电子车牌E2A9000的备案信息！</PROC_ERROR_DESCRIPTION>\n" +
                "  <TECH_ERROR_CODE/>\n" +
                "  <TECH_ERROR_DESCRIPTION/>\n" +
                "  <VE_INFO>\n" +
                "    <VE_LICENSE_NO>Test001</VE_LICENSE_NO>\n" +
                "  </VE_INFO>\n" +
                "  <CONTA_ID/>\n" +
                "  <ESEAL_ID/>\n" +
                "  <OP_HINT>没有电子车牌的备案信息</OP_HINT>\n" +
                "  <LED_HINT>海关不放行</LED_HINT>\n" +
                "  <EXTENDED_CONTENT/>\n" +
                "</GATHER_FEEDBACK>";
        ClientUtil.callWebSV(url, "Receive82Xml", cont, "0x82");

    }
}
